/*
 * Copyright 2025-2026 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package hawk.example.bookstore.repository.data;

import hawk.example.bookstore.po.AuthorPo;
import hawk.example.bookstore.po.BookPo;
import org.jeasy.random.EasyRandom;
import org.jeasy.random.EasyRandomParameters;
import org.jeasy.random.FieldPredicates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * This class is responsible for
 *
 * @author Hawk
 */
@Component
public class BookGenerator extends AbstractGenerator {

    private final EasyRandom bookPoEasyRandom = bookPoEasyRandom();
    private final String[] bookCategory = new String[]{"history", "language", "math", "chemistry", "program", "science", "nature", "biology"};

    private final LocalDateRangeGenerator publishDateGenerator = new LocalDateRangeGenerator(LocalDate.now().minusYears(200), LocalDate.now());

    public BookPo generate() {
        return bookPoEasyRandom.nextObject(BookPo.class);
    }

    public List<BookPo> generate(int size) {
        return IntStream.range(0, size)
            .mapToObj(i -> bookPoEasyRandom.nextObject(BookPo.class))
            .collect(Collectors.toList());
    }

    private EasyRandom bookPoEasyRandom() {
        EasyRandomParameters parameters = new EasyRandomParameters();
        parameters.randomize(BookPo.class, () -> {
            BookPo po = new EasyRandom(initParameters()).nextObject(BookPo.class);
            po.setUpdateUserId(po.getCreateUserId());
            po.setUpdateDateTime(po.getCreateDateTime());
            return po;
        });
        return new EasyRandom(parameters);
    }

    private EasyRandomParameters initParameters() {

        return buildEasyRandomParameters(faker, p -> {
            Predicate<Field> bookIdField = FieldPredicates.named("bookId").and(FieldPredicates.inClass(BookPo.class));
            p.randomize(bookIdField, idGenerator::next);

            Predicate<Field> titleField = FieldPredicates.named("title").and(FieldPredicates.inClass(BookPo.class));
            p.randomize(titleField, faker.book()::title);

            Predicate<Field> isbnField = FieldPredicates.named("isbn").and(FieldPredicates.inClass(BookPo.class));
            p.randomize(isbnField, ISBNGenerator::generateISBN13);

            Predicate<Field> publishDateField = FieldPredicates.named("publishDate").and(FieldPredicates.inClass(BookPo.class));
            p.randomize(publishDateField, publishDateGenerator::randomLocalDate);

            Predicate<Field> bookCategoryField = FieldPredicates.named("bookCategory").and(FieldPredicates.inClass(BookPo.class));
            p.randomize(bookCategoryField, this::randomBookCategory);

            p.excludeField(FieldPredicates.named("publisherId"));
        });
    }

    private String randomBookCategory() {
        return bookCategory[faker.random().nextInt(0,bookCategory.length-1)];
    }
}
